package com.example.pengsiyuan.ppbluetoothle.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.example.pengsiyuan.ppbluetoothle.DBManager;
import com.example.pengsiyuan.ppbluetoothle.R;
import com.example.pengsiyuan.ppbluetoothle.util.DataUtil;
import com.example.pengsiyuan.ppbluetoothle.util.PPUtil;
import com.peng.ppscalelibrary.BleManager.Interface.BleDataProtocoInterface;
import com.peng.ppscalelibrary.BleManager.Manager.BleManager;
import com.peng.ppscalelibrary.BleManager.Model.BleDeviceModel;
import com.peng.ppscalelibrary.BleManager.Model.BleUserModel;
import com.peng.ppscalelibrary.BleManager.Model.LFPeopleGeneral;

import java.util.List;

public class ScaleWeightActivity extends AppCompatActivity {
    TextView weightTextView;
    BleManager bleManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scale_weight);
        weightTextView = findViewById(R.id.weightTextView);
        bleManager = BleManager.shareInstance(getApplication());
        bindingDevice();
    }

    @Override
    protected void onPause() {
        super.onPause();
        bleManager.stopSearch();

    }

    private void bindingDevice(){

        List<BleDeviceModel> deviceList = DBManager.manager().getDeviceList();
        final BleUserModel userModel = DataUtil.util().getUserModel();
        bleManager.searchDevice(false, deviceList, userModel, new BleDataProtocoInterface() {
            @Override
            public void progressData(final LFPeopleGeneral bodyDataModel) {
                String weightStr = PPUtil.getWeight(userModel.unit, bodyDataModel.lfWeightKg);
                weightTextView.setText(weightStr);
            }

            @Override
            public void lockedData(LFPeopleGeneral bodyDataModel, BleDeviceModel deviceModel, boolean isHeartRating) {
                if (!isHeartRating){
                    String weightStr = PPUtil.getWeight(userModel.unit, bodyDataModel.lfWeightKg);
                    weightTextView.setText(weightStr);
                    DataUtil.util().setBodyDataModel(bodyDataModel);
                    dismissSelf();
                }

            }

            @Override
            public void historyData(boolean isEnd, LFPeopleGeneral bodyDataModel, String date) {
                if (!isEnd){
                    Log.d("history", "historyData: " + bodyDataModel.toString());
                }
            }

            @Override
            public void deviceInfo(BleDeviceModel deviceModel) {

            }
        });

    }

    private void dismissSelf(){
        bleManager.stopSearch();
        finish();
    }


}
